#include "RedisMgr.h"
#include "RedisConPool.h"
#include "../../Singleton.h"
#include <iostream>

void TestRedisMgr()
{
    {
        auto conn = Singleton<RedisConPool>::GetInstance()->GetConnection();
        auto b = RedisMgr::Set(conn, "test_key", "test_value");
        if (!b) {
            std::cerr << "Failed to set key in Redis server." << std::endl;
            return;
        }
    }

    {
        auto conn = Singleton<RedisConPool>::GetInstance()->GetConnection();
        auto b = RedisMgr::ExistsKey(conn, "test_key");
        if (!b) {
            std::cerr << "Key does not exist in Redis server." << std::endl;
            return;
        }
    }

    {
        auto conn = Singleton<RedisConPool>::GetInstance()->GetConnection();
        auto value = RedisMgr::Get(conn, "test_key");
        if (!value) {
            std::cerr << "Failed to get value from Redis server." << std::endl;
            return;
        }
        std::cout << "Value for 'test_key': " << *value << std::endl;
    }

    {
        auto conn = Singleton<RedisConPool>::GetInstance()->GetConnection();
        auto b = RedisMgr::Del(conn, "test_key");
        if (!b) {
            std::cerr << "Failed to delete key from Redis server." << std::endl;
            return;
        }
    }

    {
        auto conn = Singleton<RedisConPool>::GetInstance()->GetConnection();
        auto b = RedisMgr::HSet(conn, "test_hash", "field1", "value1");
        if (!b) {
            std::cerr << "Failed to set hash field in Redis server." << std::endl;
            return;
        }
    }

    {
        auto conn = Singleton<RedisConPool>::GetInstance()->GetConnection();
        auto hgetRes = RedisMgr::HGet(conn, "test_hash", "field1");
        if (!hgetRes) {
            std::cerr << "Failed to get hash field from Redis server." << std::endl;
            return;
        }
        std::cout << "Value for 'field1' in 'test_hash': " << *hgetRes << std::endl;
    }

    {
        auto conn = Singleton<RedisConPool>::GetInstance()->GetConnection();
        auto b = RedisMgr::HDel(conn, "test_hash", "field1");
        if (!b) {
            std::cerr << "Failed to delete hash field from Redis server." << std::endl;
            return;
        }
    }

    {
        auto conn = Singleton<RedisConPool>::GetInstance()->GetConnection();
        RedisMgr::LPush(conn, "test_list", "value1");
        RedisMgr::LPush(conn, "test_list", "value2");
        RedisMgr::RPush(conn, "test_list", "value3");
        std::vector<std::string> lrangeResult;
        auto lrangeRes = RedisMgr::LRange(conn, "test_list", 0, -1, lrangeResult);
        if (!lrangeRes) {
            std::cerr << "Failed to get range from list in Redis server." << std::endl;
            return;
        }
        std::cout << "Values in 'test_list': ";
        for (const auto& val : lrangeResult) {
            std::cout << val << " ";
        }
        std::cout << std::endl;
    }

    {
        auto conn = Singleton<RedisConPool>::GetInstance()->GetConnection();
        RedisMgr::RPop(conn, "test_list");
        std::vector<std::string> lrangeResult;
        auto lrangeRes = RedisMgr::LRange(conn, "test_list", 0, -1, lrangeResult);
        if (!lrangeRes) {
            std::cerr << "Failed to get range from list in Redis server." << std::endl;
            return;
        }
        std::cout << "Values in 'test_list': ";
        for (const auto& val : lrangeResult) {
            std::cout << val << " ";
        }
        std::cout << std::endl;
    }

}


int main(void)
{
    TestRedisMgr();

    return 0;
}